/**
 * window.localStorage
 * @method set 设置
 * @method get 获取
 * @method remove 移除
 * @method clear 移除全部
 */
export const Local = {
    // 从本地存储中获取数据
    get(key: string) {
        const json: any = window.localStorage.getItem(key)
        if (json === null || json === undefined || json === 'undefined') {
            return null
        }
        try {
            return JSON.parse(json)
        } catch (e) {
            console.error(`Failed to parse localStorage item with key: ${key}`, e)
            return null
        }
    },
    
    // 设置数据到本地存储
    set(key: string, val: any) {
        window.localStorage.setItem(key, JSON.stringify(val))
    },
    
    // 移除本地存储中的数据
    remove(key: string) {
        window.localStorage.removeItem(key)
    },
    
    // 清空所有本地存储
    clear() {
        window.localStorage.clear()
    },
}

/**
 * window.sessionStorage
 * @method set 设置会话缓存
 * @method get 获取会话缓存
 * @method remove 移除会话缓存
 * @method clear 移除全部会话缓存
 */
export const Session = {
    set(key: string, val: any) {
        window.sessionStorage.setItem(key, JSON.stringify(val))
    },
    get(key: string) {
        const json: any = window.sessionStorage.getItem(key)
        if (json === null || json === undefined || json === 'undefined') {
            return null
        }
        try {
            return JSON.parse(json)
        } catch (e) {
            console.error(`Failed to parse sessionStorage item with key: ${key}`, e)
            return null
        }
    },
    remove(key: string) {
        window.sessionStorage.removeItem(key)
    },
    clear() {
        window.sessionStorage.clear()
    },
}
